package cn.iocoder.yudao.module.wine.dal.mysql.businessuser;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.wine.controller.app.businessuser.vo.AppUserAddressRespVO;
import cn.iocoder.yudao.module.wine.dal.dataobject.businesscredit.BusinessCreditDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.businessuser.BusinessUserDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.wine.controller.admin.businessuser.vo.*;

import java.util.Collection;
import java.util.List;

/**
 * 商家配置 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface BusinessUserMapper extends BaseMapperX<BusinessUserDO> {

    default PageResult<BusinessUserRespVO> selectPage(BusinessUserPageReqVO reqVO) {
        return selectJoinPage(reqVO,BusinessUserRespVO.class, new MPJLambdaWrapperX<BusinessUserDO>()
                .eqIfPresent(BusinessUserDO::getBusinessType, reqVO.getBusinessType())
                .eqIfPresent(BusinessUserDO::getAreaId, reqVO.getAreaId())
                .eqIfPresent(BusinessUserDO::getParentId, reqVO.getParentId())
                .eqIfPresent(BusinessUserDO::getUserId, reqVO.getUserId())
                .eqIfPresent(BusinessUserDO::getUserType, reqVO.getUserType())
                .eqIfPresent(BusinessUserDO::getCreditId, reqVO.getCreditId())
                .likeIfPresent(BusinessUserDO::getCompanyName, reqVO.getCompanyName())
                .eqIfPresent(BusinessUserDO::getCompanyImage, reqVO.getCompanyImage())
                .eqIfPresent(BusinessUserDO::getCardImage, reqVO.getCardImage())
                .eqIfPresent(BusinessUserDO::getLiveImage, reqVO.getLiveImage())
                .eqIfPresent(BusinessUserDO::getLicenseImage, reqVO.getLicenseImage())
                .eqIfPresent(BusinessUserDO::getReceiveAddress, reqVO.getReceiveAddress())
                .eqIfPresent(BusinessUserDO::getDeliveryAddress, reqVO.getDeliveryAddress())
                .eqIfPresent(BusinessUserDO::getSupplyNum, reqVO.getSupplyNum())
                .eqIfPresent(BusinessUserDO::getBuyNum, reqVO.getBuyNum())
                .eqIfPresent(BusinessUserDO::getComplainNum, reqVO.getComplainNum())
                .eqIfPresent(BusinessUserDO::getLeaveNum, reqVO.getLeaveNum())
                .eqIfPresent(BusinessUserDO::getStatus, reqVO.getStatus())
                .eqIfPresent(BusinessUserDO::getIsAuth, reqVO.getIsAuth())
                .eqIfPresent(BusinessUserDO::getIsOpen, reqVO.getIsOpen())
                .betweenIfPresent(BusinessUserDO::getAuthTime, reqVO.getAuthTime())
                .betweenIfPresent(BusinessUserDO::getOverTime, reqVO.getOverTime())
                .betweenIfPresent(BusinessUserDO::getCreateTime, reqVO.getCreateTime())

                .selectAll(BusinessUserDO.class)

                .selectAs(BusinessCreditDO::getId,BusinessUserRespVO::getCreditId)
                .selectAs(BusinessCreditDO::getName,BusinessUserRespVO::getCreditName)

                .selectAs(MemberUserDO::getMobile,BusinessUserRespVO::getMobile)
                .selectAs(MemberUserDO::getName,BusinessUserRespVO::getUsername)
                .selectAs(MemberUserDO::getNickname,BusinessUserRespVO::getNickname)
                .selectAs(MemberUserDO::getAudit,BusinessUserRespVO::getAudit)

                .leftJoin(BusinessCreditDO.class,BusinessCreditDO::getId,BusinessUserDO::getCreditId)
                .leftJoin(MemberUserDO.class,MemberUserDO::getId,BusinessUserDO::getUserId)
                .eq(MemberUserDO::getIsBusiness,1)
                .likeIfExists(MemberUserDO::getName, reqVO.getUsername())
                .likeIfExists(MemberUserDO::getNickname, reqVO.getNickname())
                .likeIfExists(MemberUserDO::getMobile,reqVO.getMobile())
                .orderByDesc(BusinessUserDO::getId));
    }

    default AppUserAddressRespVO getBusinessUserByMobile(String mobile){
        return selectJoinOne(AppUserAddressRespVO.class,new MPJLambdaWrapperX<BusinessUserDO>()
                .leftJoin(MemberUserDO.class,MemberUserDO::getId,BusinessUserDO::getUserId)
                .eq(MemberUserDO::getMobile,mobile)
                .selectAll(BusinessUserDO.class)
                .selectAs(MemberUserDO::getMobile,AppUserAddressRespVO::getMobile)
                .selectAs(MemberUserDO::getName,AppUserAddressRespVO::getRealName)
                .selectAs(MemberUserDO::getNickname,AppUserAddressRespVO::getNickname)
                .selectAs(MemberUserDO::getIsTeam,AppUserAddressRespVO::getIsTeam)
                .selectAs(MemberUserDO::getTeamOverTime,AppUserAddressRespVO::getTeamOverTime)
                .selectAs(MemberUserDO::getTeamExpirationTime,AppUserAddressRespVO::getExpirationTime)
        );
    }

    default BusinessUserDO getUserByUserId(Long userId){
        return selectOne(new LambdaQueryWrapperX<BusinessUserDO>()
                .eqIfPresent(BusinessUserDO::getUserId, userId)
                .last("limit 1")
        );
    }

    default List<BusinessUserDO> getUserByUserIds(Collection<Long> ids){
        return selectList(new LambdaQueryWrapperX<BusinessUserDO>()
                .in(BusinessUserDO::getUserId, ids));

    }
}
